package com.example.downloader.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.downloader.R;
import com.example.downloader.model.BaseDownloader;
import com.example.downloader.model.DownloadInfo;
import com.example.downloader.model.ProgressAware;
import com.example.downloader.model.ProgressHolder;
import com.example.downloader.upload.Callback;
import com.example.downloader.upload.MyUploader;

import java.util.List;


/**
 * Created by Administrator on 2017/8/3 0003.
 */

public class UploadAdapter extends BaseAdapter {
    MyUploader mDownloader;
    private List<DownloadInfo> mData;
    private Context mContext;

    public UploadAdapter(Context context, List<DownloadInfo> data, MyUploader util) {
        mContext = context;
        mData = data;
        mDownloader = util;
    }


//    @Override
//    void setHolder(final int position, final ViewHolder viewHolder) {
//        final CourseData.DataBean ac = (CourseData.DataBean) getItem(position);
//
//        final CourseData.DataBean.AttachmentBean attachmentBean = ac.getAttachment();
//        final int courseId = attachmentBean == null ? -1 : attachmentBean.getId();
//        CourseAware courseAware = new CourseAware(viewHolder);
//        mDownloader.loadingDownloadState(courseAware, courseId);
//        viewHolder.attachmentname.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (onClickListenerEditOrDelete != null) {
////                    final String filePath = getDownloadFilePath(attachmentBean);
//                    switch (mDownloader.getWorkingState(courseId)) {
//                        case FLAG_FREE:
//                        case FLAG_ERROR:
//                        case FLAG_CANCLED:
//                            onClickListenerEditOrDelete.onDownLoad(position, ac, viewHolder);
//                            break;
//                        case FLAG_FINISHED:
//                            onClickListenerEditOrDelete.onOpen(position, ac, viewHolder);
//                            break;
//                    }
//                }
//            }
//        });
//    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public DownloadInfo getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_download, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final DownloadInfo downloadInfo = getItem(position);
        viewHolder.title.setText(downloadInfo.name);
//        mDownloader.loadingDownloadState(new DwonloadAware(viewHolder), downloadInfo);
        switch (downloadInfo.status) {
            case DownloadInfo.STARTED:
                mDownloader.load(downloadInfo.uri, null);
                break;
            case DownloadInfo.ERROR:
                viewHolder.status.setText("错误");
                viewHolder.progressBar.setProgress(0);
                break;
            case DownloadInfo.SUCCESS:
                viewHolder.status.setText("已完成");
                viewHolder.progressBar.setProgress(100);
                break;
            case DownloadInfo.CANCEL:
                viewHolder.status.setText("已取消");
                viewHolder.progressBar.setProgress(0);

                break;
        }

        return convertView;
    }

    static class ViewHolder extends ProgressHolder {
        ViewHolder(View view) {
            progressBar = (ProgressBar) view.findViewById(R.id.progress);
            status = (TextView) view.findViewById(R.id.status);
            title = (TextView) view.findViewById(R.id.title);
        }

        TextView status;
        TextView title;
    }


    public static class DwonloadAware extends ProgressAware {
        public DwonloadAware(ProgressHolder progressHolder) {
            super(progressHolder);
        }

        @Override
        protected void setFreeState(boolean validated) {
            final ViewHolder views = (ViewHolder) getProgressHolder();
            if (views != null) {
                if (validated) {
                    views.status.setText("下载");
                    views.progressBar.setVisibility(View.GONE);
                } else {
                    views.status.setText("无法下载");
                    views.progressBar.setVisibility(View.GONE);
                }
            }
        }

        @Override
        protected void setReadyState() {
            final ViewHolder views = (ViewHolder) getProgressHolder();
            if (views != null) {
                views.status.setText("正在准备...");
                views.progressBar.setVisibility(View.GONE);
            }
        }

        @Override
        protected void setDownloadingState(int progress) {
            final ViewHolder views = (ViewHolder) getProgressHolder();

            if (views != null) {
                views.status.setText("正在下载...");
                views.progressBar.setVisibility(View.VISIBLE);
                views.progressBar.setProgress(progress);
            }
        }

        @Override
        protected void setDownloadingFinishedState(DownloadInfo downloadInfo, String downloadPath) {
            final ViewHolder views = (ViewHolder) getProgressHolder();
            if (views != null) {
                views.status.setText("下载完成");
                views.progressBar.setVisibility(View.GONE);
                views.progressBar.setProgress(100);
            }
        }


        @Override
        protected void setErrorState() {
            final ViewHolder views = (ViewHolder) getProgressHolder();
            if (views != null) {
                views.status.setText("下载失败");
                views.progressBar.setVisibility(View.GONE);
                views.progressBar.setProgress(0);
            }
        }

        @Override
        protected void setCancledState() {
            final ViewHolder views = (ViewHolder) getProgressHolder();
            if (views != null) {
                views.status.setText("已取消");
                views.progressBar.setVisibility(View.GONE);
                views.progressBar.setProgress(0);
            }
        }
    }


//    public boolean downloadAsync(final CourseData.DataBean.AttachmentBean dataBean, ViewHolder viewHolder) {
//        return mDownloader.downloadFile(new CourseAware(viewHolder), dataBean == null ? null : new DownloadInfo(dataBean.getId(), dataBean.getUrl(), dataBean.getName()));
//    }
//
//    public String getDownloadPath(final CourseData.DataBean.AttachmentBean dataBean) {
//        return mDownloader.getDownloadPath(dataBean == null ? null : new DownloadInfo(dataBean.getId(), dataBean.getUrl(), dataBean.getName()));
//    }


}
